package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpEducationAddDTO;
import org.jsola.hr.dto.EmpEducationUpdateDTO;
import org.jsola.hr.query.EmpEducationQuery;
import org.jsola.hr.service.IEmpEducationService;
import org.jsola.hr.vo.EmpEducationListVO;
import org.jsola.hr.vo.EmpEducationVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrEmpEducationController")
@RequestMapping(HrConstants.API_PATH + "/v1/empEducations")
public class EmpEducationController implements IEmpEducationControllerAPI {

    @Autowired
    private IEmpEducationService empEducationService;

    @Override
    @PostMapping
    public Result<EmpEducationVO> save(@Valid @RequestBody EmpEducationAddDTO empEducationAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工教育经历权限校验

        return Result.success(
                empEducationService.save(empEducationAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpEducationUpdateDTO empEducationUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工教育经历权限校验

        int count = empEducationService.update(empEducationUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{empEducationId}")
    public Result<Integer> delete(@PathVariable String empEducationId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工教育经历权限校验

        int count = empEducationService.deleteByIds(tokenUser, empEducationId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empEducationIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工教育经历权限校验

        int count = empEducationService.deleteByIds(tokenUser, empEducationIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empEducationId}")
    public Result<EmpEducationVO> findById(@PathVariable String empEducationId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工教育经历权限校验

        return Result.success(
            empEducationService.selectById(empEducationId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpEducationListVO>> listAll(@Valid EmpEducationQuery empEducationQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工教育经历权限校验

        return Result.success(
            empEducationService.select(empEducationQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpEducationListVO>> page(@Valid EmpEducationQuery empEducationQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            empEducationService.selectPage(empEducationQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @DeleteMapping(value = "/deleteEmpEducation/{empEducationId}")
    public Result<Integer> deleteEmpEducation(@PathVariable String empEducationId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工教育经历权限校验

        Integer count = empEducationService.deleteEmpEducation(tokenUser, empEducationId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


}

